package org.tigris.atlas.examples.service;

import org.tigris.atlas.examples.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.examples.transfer.CompositeEntity;
import org.tigris.atlas.examples.transfer.CompositeEntityPK;
import org.tigris.atlas.examples.transfer.TransferObjectFactory;
import org.tigris.atlas.messages.Messages;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import junit.framework.TestCase;

public class TestCompositeEntity extends TestCase {

	private EntityMaintenanceServiceDelegate delegate;
	
	private static Long id;
	
	protected void setUp() throws Exception {
		super.setUp();
		
		delegate = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
	}
	
	protected void tearDown() {
		if( id != null ) {
			CompositeEntityPK pk = TransferObjectFactory.createCompositeEntityPK();
			pk.setId( id );
			delegate.delete( pk );
		}
		
		id = null;
	}

	public void testCreate() {
		CompositeEntity ce = TransferObjectFactory.createCompositeEntity();
		ce.setName("foo");
		ce.getMiscInfoComposite().setDecimalField(new BigDecimal("0"));
		ce.getMiscInfoComposite().setStringField("thisisavalidvalue");
		
		CompositeEntityServiceResponse response = delegate.save(ce);
		assertFalse(response.getMessages().hasErrorMessages());
		
		if (response.getCompositeEntity().getAllMessages().hasErrorMessages()) {
			Messages messages = response.getCompositeEntity().getAllMessages(); 
			Collection errorCollection = messages.getErrorMessages();
			Iterator i = errorCollection.iterator();
			while (i.hasNext()) {
				System.out.println(i.next());
			}			
				
		}
		
		assertFalse(response.getCompositeEntity().getAllMessages().hasErrorMessages());

		ce = response.getCompositeEntity();
		CompositeEntityPK pk = TransferObjectFactory.createCompositeEntityPK();
		pk.setId(ce.getId());
		id = ce.getId();
		
		response = delegate.findByPrimaryKey(pk);
		assertFalse(response.getMessages().hasErrorMessages());
		assertNotNull(response.getCompositeEntity());
		
		ce = response.getCompositeEntity();
		assertEquals(0, ce.getMiscInfoComposite().getDecimalField().intValue());
		assertEquals("thisisavalidvalue", ce.getMiscInfoComposite().getStringField());
	}
	
	public void testCreateWithCompositeErrors() {
		CompositeEntity ce = TransferObjectFactory.createCompositeEntity();
		ce.setName("foo");
		ce.getFooComposite().setStringField("invalid");
		
		CompositeEntityServiceResponse response = delegate.save(ce);
		assertFalse(response.getMessages().hasErrorMessages());
		ce = response.getCompositeEntity();
		assertNull(ce.getId());
		assertTrue(ce.getAllMessages().hasErrorMessages());
		assertFalse(ce.getMessages().hasErrorMessages());
		assertTrue(ce.getFooComposite().getMessages().hasErrorMessages());
		assertFalse(ce.getMiscInfoComposite().getMessages().hasErrorMessages());
	}
	
	public void testCreateWithDeclarativeValidationErrors() {
		CompositeEntity ce = TransferObjectFactory.createCompositeEntity();
		ce.setName("foo");
		ce.getMiscInfoComposite().setDecimalField(new BigDecimal("0"));
		ce.getMiscInfoComposite().setStringField("invalid");
		
		CompositeEntityServiceResponse response = delegate.save(ce);
		assertFalse(response.getMessages().hasErrorMessages());
		ce = response.getCompositeEntity();
		id = ce.getId();
		assertNull(ce.getId());
		assertTrue(ce.getAllMessages().hasErrorMessages());
		assertFalse(ce.getMessages().hasErrorMessages());
		assertFalse(ce.getFooComposite().getMessages().hasErrorMessages());
		assertTrue(ce.getMiscInfoComposite().getMessages().hasErrorMessages());
	}
	
	public void testCreateWithAllCompositeFieldsPopulated() {
		CompositeEntity ce = TransferObjectFactory.createCompositeEntity();
		ce.setName("foo");
		ce.getMiscInfoComposite().setDecimalField(new BigDecimal("0"));
		ce.getMiscInfoComposite().setStringField("thisisavalidvalue");
		ce.getMiscInfoComposite().setDateField(new Date());
		ce.getMiscInfoComposite().setLongField(new Long(2));
		ce.getMiscInfoComposite().setTimestampField(new Timestamp(System.currentTimeMillis()));
		
		CompositeEntityServiceResponse response = delegate.save(ce);
		assertFalse(response.getMessages().hasErrorMessages());
		ce = response.getCompositeEntity();
		id = ce.getId();
		assertNotNull(ce.getId());
		assertFalse(ce.getAllMessages().hasErrorMessages());		
	}
	
}
